<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统设置</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="queryinfo" class="demo-form-inline" size="mini">
        <el-form-item>
          <el-select v-model="queryinfo.comid" placeholder="所属公司" clearable filterable>
            <el-option
                v-for="item in company"
                :key="item.comid"
                :label="item.title"
                :value="item.comid"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryinfo.statu" clearable filterable>
            <el-option label="正常" value="1"></el-option>
            <el-option label="拉黑" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间选择">
          <el-date-picker
              v-model="timer"
              type="datetimerange"
              range-separator="-"
              start-placeholder="Start date"
              end-placeholder="End date"
              :default-time="defaultTime"
              @change="changeData"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="GetData">查询</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="DataOpen(1)">创建</el-button>
        </el-form-item>
      </el-form>
      <hr>
      <!--数据-->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="title" label="公司" width="180" />
        <el-table-column prop="class_name" label="分类名称" width="180" />
        <el-table-column prop="class_info" label="分类说明" width="180" />
        <el-table-column label="状态" width="180" >
          <template #default="props">
            <el-tag size="mini" v-if="props.row.statu === 1" type="success">正常</el-tag>
            <el-tag size="mini" v-else type="warning">关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="addtime" label="注册时间" >
          <template #default="props">{{ formatDate(props.row.addtime) }}</template>
        </el-table-column>
        <el-table-column prop="uptime" label="更新时间" >
          <template #default="props">{{ formatDate(props.row.uptime) }}</template>
        </el-table-column>
        <el-table-column label="##">
          <template #default="props">
            <el-button type="primary" @click="DataOpen(props.row)" size="mini">Edit</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
          background
          :current-page="queryinfo.page"
          :page-sizes="[3,10, 50, 100, 1000]"
          :page-size="queryinfo.size"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="changePage"
      />
    </el-card>


    <!--      添加修改-->
    <el-drawer
        v-model="DataDrawer"
        :title="DataTitle === 1?'添加':'更新'"
        direction="rtl"
        :before-close="DataClose"
    >
      <el-form  :model="DataForm" label-width="120px">

        <el-form-item label="所属公司">
          <el-select v-model="DataForm.comid" placeholder="所属公司" clearable filterable>
            <el-option
                v-for="item in company"
                :key="item.comid"
                :label="item.title"
                :value="item.comid"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称">
          <el-input v-model="DataForm.class_name" ></el-input>
        </el-form-item>
        <el-form-item label="分类说明">
          <el-input v-model="DataForm.class_info" ></el-input>
        </el-form-item>
        <el-form-item label="分类状态">
          <el-switch v-model="DataForm.statu" active-color="#13ce66" inactive-color="#ff4949" :active-value='1'
                     :inactive-value='2' />
        </el-form-item>
        <el-form-item>
          <el-button  type="success" @click="DataSumbit">{{ DataForm === 1 ? "Create":"Updata" }}</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from "vue";
import {ElMessage} from "element-plus";
import axios from "@/utils/axios";
import {formatDate} from '@/utils/date'
export default {
  setup() {
    const state = reactive({
      queryinfo: {
        page: 1,
        size: 10
      },
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      total: 0,
      tableData: [],

      //时间
      timer: [],
      defaultTime: [
        new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),
      ],

      company:[],

      DataForm: {},
      DataDrawer: false,
      DataTitle: 1,

    })
    onMounted(() => {
      GetData();GetCompany()
    })


    //时间操作
    const changeData = async (row) => {
      if (row !== null) {
        state.queryinfo.startTime = ((new Date(row[0])).getTime()) / 1000
        state.queryinfo.endTime = ((new Date(row[1])).getTime()) / 1000
      } else {
        delete state.queryinfo.startTime
        delete state.queryinfo.endTime
      }
    }


    const GetCompany = async () => {
      const {data: res} = await axios.get('user/company')
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.company = res.data
    }


    //打开关闭抽屉
    const DataOpen = (data) => {
      state.DataDrawer = true
      if (data === 1){
        state.DataForm = {statu:2}
        state.DataTitle = 1
      }else{
        state.DataForm = data
        state.DataTitle = 2
      }
    }
    const DataClose = () => {
      state.DataDrawer = false
    }
    //提交更新/新加
    const DataSumbit = async () => {
      if (state.DataTitle ===1){
        const {data: res} = await axios.put('news/class',  state.DataForm)
        if (res.code !== 1000) return ElMessage.error(res.msg)
      }else{
        const {data: res} = await axios.post('news/class',  state.DataForm)
        if (res.code !== 1000) return ElMessage.error(res.msg)
      }
      ElMessage.success("成功")
      state.DataDrawer = false
    }


    // 获取列表
    const GetData = async () => {
      const {data: res} = await axios.get('news/class', {params: state.queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.tableData = res.data.list
      state.total = res.data.total
    }
    const changePage = (val) => {
      state.queryinfo.page = val
      GetData()
    }
    const handleSizeChange = (val) => {
      state.queryinfo.size = val
      GetData()
    }

    return {
      ...toRefs(state),
      GetData,
      formatDate,
      changeData,
      changePage,
      handleSizeChange,

      DataOpen,DataClose,DataSumbit
    }
  }
}
</script>

<style scoped>

</style>